package linc.cool.usage;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author yqlin
 * @date 2021/12/17 2:30 AM
 * @description
 */
@Slf4j
public class 两个线程分别同时访问静态synchronized和非静态synchronized方法 implements Runnable {
    private static final 两个线程分别同时访问静态synchronized和非静态synchronized方法 INSTANCE = new 两个线程分别同时访问静态synchronized和非静态synchronized方法();

    @Override
    public void run() {
        String currentThread = Thread.currentThread().getName();
        if ("t1".equals(currentThread)) {
            staticSyncMethod();
        }
        if ("t2".equals(currentThread)) {
            syncMethod();
        }
    }

    @SneakyThrows
    private static synchronized void staticSyncMethod() {
        log.debug("staticSyncMethod {} 执行开始", Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(4);
        log.debug("staticSyncMethod {} 执行结束", Thread.currentThread().getName());
    }

    @SneakyThrows
    private synchronized void syncMethod() {
        log.debug("syncMethod {} 执行开始", Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(4);
        log.debug("syncMethod {} 执行结束", Thread.currentThread().getName());
    }

    @SneakyThrows
    public static void main(String[] args) {
        Thread t1 = new Thread(INSTANCE, "t1");
        Thread t2 = new Thread(INSTANCE, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.debug("end");
    }
}
